//
//  BBSViewController.m
//  CarHome
//
//  Created by 小伍 on 14-9-22.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//
#import "ProvinceModel_province.h"
#import "BBSViewController.h"
#import "ThreeTableViewCell_Froum.h"
#import "ThreeWebView.h"
NSString * const nameLast = @"last";
NSString * const nameNew = @"new";
NSString * const nameCream = @"cream";
@interface BBSViewController ()
@property (nonatomic , retain) UISegmentedControl * segment;
@property (nonatomic , retain) UIView * viewLast;
@property (nonatomic , retain) UIView * viewNew;
@property (nonatomic , retain) UIView * viewCream;
@property (nonatomic , retain) UITableView * tableViewLast;
@property (nonatomic , retain) UITableView * tableViewNew;
@property (nonatomic , retain) UITableView * tableViewCream;
@property (nonatomic , retain) NSMutableArray * arrayProvince;//为了给重建池赋值

@end
@implementation BBSViewController

- (void)dealloc
{
    [_viewCream release];
    _viewCream = nil;
    [_viewLast release];
    _viewLast = nil;
    [_viewCream release];
    _viewCream = nil;
    [_segment release];
    _segment = nil;
    [_tableViewCream release];
    _tableViewCream = nil;
    [_tableViewLast release];
    _tableViewLast = nil;
    [_tableViewNew release];
    _tableViewNew = nil;
    [_arrayProvince release];
    _arrayProvince = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _arrayProvince = [[NSMutableArray alloc]init];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *leftBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBack setFrame:CGRectMake(0, 0, 25, 25)];
    [leftBack setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
    [leftBack addTarget:self action:@selector(leftBackMethod:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBack];
    [self.navigationItem setTitle:_bbsname];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self.navigationController.navigationBar setTitleTextAttributes:dict];
    // Do any additional setup after loading the view.
    [self connect];
    
}
#pragma mark -
#pragma mark UIview 创建
- (void)creatView
{
    self.viewLast = [[UIView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height)];
    [self.viewLast setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.viewLast];
    
    
    self.viewNew = [[UIView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height)];
    [self.viewNew setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:self.viewNew];
    
    
     self.viewCream = [[UIView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height)];
    [self.viewCream setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:self.viewCream];
    [self.view bringSubviewToFront:self.viewLast];
    
}
#pragma mark -
#pragma mark UISegmenControl 创建
- (void)creatSegment
{
    NSArray * array = [[NSArray alloc]initWithObjects:@"最后回复",@"最新发布",@"精华帖", nil];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    [self.segment setSelectedSegmentIndex:0];
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.segment setFrame:CGRectMake(40 ,5, 240, 30)];
    
    UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    [self.segment setTitleTextAttributes:attributes
                           forState:UIControlStateNormal];
    UIColor * color = [UIColor colorWithRed:160/255.0 green:17/255.0 blue:27/255.0 alpha:1];
    [self.segment setTintColor:color];//颜色
    [self.segment setWidth:80 forSegmentAtIndex:0];
    [self.segment setWidth:80 forSegmentAtIndex:1];
    [self.segment setWidth:80 forSegmentAtIndex:2];
    
    [self.view addSubview:_segment];
    
}
#pragma mark - 
#pragma mark UISegmenControl 点击事件
- (void)segmentAction:(id)sender
{
    switch (_segment.selectedSegmentIndex) {
        case 0:
            _o = 0;
            _r = 0;
            [_arrayProvince removeAllObjects];
            [self connectfresh];
            
            [self.view bringSubviewToFront:self.viewLast];
            NSLog(@"00000");
            break;
        case 1:
            [self.view bringSubviewToFront:self.viewNew];
            _o = 2;
            _r = 0;
            [_arrayProvince removeAllObjects];
            [self connectfresh];
        
            NSLog(@"11111");
            break;
        case 2:
            [self.view bringSubviewToFront:self.viewCream];
            _o = 0;
            _r = 1;
            [_arrayProvince removeAllObjects];
            [self connectfresh];
            
          
            NSLog(@"22222");
            break;
           
        default:
            break;
    }
}
- (void)button
{
    UIButton *TiShi = [UIButton buttonWithType:UIButtonTypeCustom];
    [TiShi setFrame:CGRectMake(20, 100, 280, 60)];
    [TiShi setBackgroundColor:[UIColor grayColor]];
    [TiShi setTitle:@"暂无精品" forState:UIControlStateNormal];
    [TiShi addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:TiShi];
    NSLog(@"暂无暂无暂无暂无暂无暂无暂无暂无暂无暂无暂无暂无");

}
- (void)buttonAction:(UIButton *)sender
{
    [sender removeFromSuperview];
}
#pragma mark -
#pragma mark TableView 创建
- (void)creatTableView
{
    self.tableViewLast = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableViewLast.delegate = self;
    self.tableViewLast.dataSource = self;
    [self.tableViewLast setSeparatorColor:[UIColor redColor]];
    [self.tableViewLast setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.tableViewLast registerClass:[ThreeTableViewCell_Froum class] forCellReuseIdentifier:nameLast];
    [self.viewLast addSubview:self.tableViewLast];
    
    
    
    self.tableViewNew = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableViewNew.delegate = self;
    self.tableViewNew.dataSource = self;
    [self.tableViewNew setSeparatorColor:[UIColor redColor]];
    [self.tableViewNew setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.tableViewNew registerClass:[ThreeTableViewCell_Froum class] forCellReuseIdentifier:nameNew];
    [self.viewNew addSubview:self.tableViewNew];
    
    
    
    self.tableViewCream = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableViewCream.delegate = self;
    self.tableViewCream.dataSource = self;
    [self.tableViewCream setSeparatorColor:[UIColor redColor]];
    [self.tableViewCream setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.tableViewCream registerClass:[ThreeTableViewCell_Froum class] forCellReuseIdentifier:nameCream];
    [self.viewCream addSubview:self.tableViewCream];
    
    [self setupRefresh];
    [self setupRefresh_new];
    [self setupRefresh_cream];
}
#pragma mark -
#pragma mark TableView 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_arrayProvince count];
}
#pragma mark -
#pragma mark TableView 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark -
#pragma mark TableView 重建池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableViewLast) {
        ThreeTableViewCell_Froum * cell = [tableView dequeueReusableCellWithIdentifier:nameLast];
        if ([_arrayProvince count] != 0) {
            
            ProvinceModel_province * model = [_arrayProvince objectAtIndex:indexPath.row];
            NSString * huiFu = [NSString stringWithFormat:@"%@回",model.replycount];
            [cell.labelTittle setText:model.tittle];
            [cell.labelReplycount setText:huiFu];
            [cell.labelPostusername setText:model.postusername];
            [cell.labelLastreplydate setText:model.lastreplydate];
            [cell.labelReplycount setFont:[UIFont systemFontOfSize:11]];
            [cell.labelPostusername setFont:[UIFont systemFontOfSize:11]];
            [cell.labelLastreplydate setFont:[UIFont systemFontOfSize:11]];
        }
        
        
        return cell;
    }
    if (tableView == _tableViewNew) {
        ThreeTableViewCell_Froum * cell = [tableView dequeueReusableCellWithIdentifier:nameNew];
        if ([_arrayProvince count] != 0) {
            
            ProvinceModel_province * model = [_arrayProvince objectAtIndex:indexPath.row];
            NSString * huiFu = [NSString stringWithFormat:@"%@回",model.replycount];
            [cell.labelTittle setText:model.tittle];
            [cell.labelReplycount setText:huiFu];
            [cell.labelPostusername setText:model.postusername];
            [cell.labelLastreplydate setText:model.lastreplydate];
            [cell.labelReplycount setFont:[UIFont systemFontOfSize:11]];
            [cell.labelPostusername setFont:[UIFont systemFontOfSize:11]];
            [cell.labelLastreplydate setFont:[UIFont systemFontOfSize:11]];
        }
        return cell;
    }
    if (tableView == _tableViewCream) {
        ThreeTableViewCell_Froum * cell = [tableView dequeueReusableCellWithIdentifier:nameCream];
        if ([_arrayProvince count] != 0) {
            
            ProvinceModel_province * model = [_arrayProvince objectAtIndex:indexPath.row];
            NSString * huiFu = [NSString stringWithFormat:@"%@回",model.replycount];
            [cell.labelTittle setText:model.tittle];
            [cell.labelReplycount setText:huiFu];
            [cell.labelPostusername setText:model.postusername];
            [cell.labelLastreplydate setText:model.lastreplydate];
            [cell.labelReplycount setFont:[UIFont systemFontOfSize:11]];
            [cell.labelPostusername setFont:[UIFont systemFontOfSize:11]];
            [cell.labelLastreplydate setFont:[UIFont systemFontOfSize:11]];
        }
        return cell;
    }
    return 0;
    
}
#pragma mark -
#pragma mark TableView 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProvinceModel_province * model = [_arrayProvince objectAtIndex:indexPath.row];
    NSString * name = [NSString stringWithFormat:@"http://forum.app.autohome.com.cn/autov4.2/forum/club/topiccontent-a2-pm1-v4.2.0-t%@-o0-p1-s20-c1-nt0-fs0-sp0-al0-cw320.html",model.topicid];
    ThreeWebView * threeweb = [[ThreeWebView alloc]init];
    [self.navigationController setTitle:model.tittle];
    
    threeweb.name = name;
    [self.navigationController pushViewController:threeweb animated:YES];

}
#pragma mark -
#pragma mark Connect网络请求
- (void)connect
{
    
    /// 地区
    NSString * url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p1-s20.html",_bbsid,_r,_o];
    NSLog(@"%@", url);
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
        }
        [self creatView];
        [self creatSegment];
        [self creatTableView];
        [_tableViewLast headerEndRefreshing];
        [_tableViewLast footerEndRefreshing];
    }];
}
- (void)connectfresh
{
    /// 地区
    NSString * url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p1-s20.html",_bbsid,_r,_o];
    NSLog(@"%@", url);
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
        }
        if (![_arrayProvince count]) {
            [self button];
        }
        [_tableViewLast reloadData];
        [_tableViewCream reloadData];
        [_tableViewNew reloadData];
    }];
}
#pragma mark -
#pragma mark setRefresh上拉加载 下拉刷新 LAST
- (void)setupRefresh
{
    [_tableViewLast addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    [_tableViewLast headerBeginRefreshing]; //程序开始即刷新
    
    [_tableViewLast addFooterWithTarget:self action:@selector(footerRereshing)];
    
    _tableViewLast.headerPullToRefreshText = @"下拉刷新";
    _tableViewLast.headerReleaseToRefreshText = @"释放刷新";
    _tableViewLast.headerRefreshingText = @"正在刷新中···";
    _tableViewLast.footerPullToRefreshText = @"上拉加载";
    _tableViewLast.footerReleaseToRefreshText = @"释放加载";
    _tableViewLast.footerRefreshingText = @"正在加载中···";
}
// *******************************************

- (void)headerRereshing
{
    [self connect_last];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_tableViewLast reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tableViewLast headerEndRefreshing];
        NSLog(@"结束刷新");
    });
}
static int a = 1;
- (void)footerRereshing
{
    NSMutableString * urlstr = [NSMutableString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%d-bto-r%d-ss0-o%d-p%d-s20.html",_bbsid,_r,_o ,a];

    NSRange subStr = [urlstr rangeOfString:[NSString stringWithFormat:@"p%d", a]];
    a++;
    if (subStr.location != NSNotFound) {
        [urlstr replaceCharactersInRange:subStr withString:[NSString stringWithFormat:@"p%d", a]];
    }
    [self connectFooter:urlstr];

    NSLog(@"%@",urlstr);
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_tableViewLast reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tableViewLast footerEndRefreshing];
        NSLog(@"结束加载");
    });
}
- (void)connectFooter:(NSString *)urlstr
{
    
  
    [NetUrl connect:urlstr parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
        }
        if (![_arrayProvince count]) {
            [self button];
        }
        [_tableViewLast reloadData];
        [_tableViewCream reloadData];
        [_tableViewNew reloadData];
    }];


}
/////////上拉加载NEW.///////
#pragma mark -
#pragma mark setRefresh上拉加载 下拉刷新 NEW
- (void)setupRefresh_new
{
    [_tableViewNew addHeaderWithTarget:self action:@selector(headerRereshing_new)];
    
    [_tableViewNew headerBeginRefreshing]; //程序开始即刷新
    
    [_tableViewNew addFooterWithTarget:self action:@selector(footerRereshing_new)];
    
    _tableViewNew.headerPullToRefreshText = @"下拉刷新啦" ;
    _tableViewNew.headerReleaseToRefreshText = @"松开吧";
    _tableViewNew.headerRefreshingText = @"正在刷新中···";
    
    _tableViewNew.footerPullToRefreshText = @"上拉加载";
    _tableViewNew.footerReleaseToRefreshText = @"松开吧";
    _tableViewNew.footerRefreshingText = @"正在加载中...";
}
// *******************************************

- (void)headerRereshing_new
{
     [self connect_new];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_tableViewNew reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tableViewNew headerEndRefreshing];
        NSLog(@"结束刷新");
    });
}
static int new = 1;
- (void)footerRereshing_new
{
    NSMutableString * urlstr = [NSMutableString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p%d-s20.html",_bbsid,_r,_o ,new];
    
    NSRange subStr = [urlstr rangeOfString:[NSString stringWithFormat:@"p%d", new]];
    new++;
    if (subStr.location != NSNotFound) {
        [urlstr replaceCharactersInRange:subStr withString:[NSString stringWithFormat:@"p%d", new]];
    }
    [self connectFooter_new:urlstr];
    
    NSLog(@"%@",urlstr);
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_tableViewNew reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tableViewNew footerEndRefreshing];
        NSLog(@"结束加载");
    });
}
- (void)connectFooter_new:(NSString *)urlstr
{
    
    
    [NetUrl connect:urlstr parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
        }
        if (![_arrayProvince count]) {
            [self button];
        }
        [_tableViewLast reloadData];
        [_tableViewCream reloadData];
        [_tableViewNew reloadData];
    }];
    
    
}
/////////Cream.///////////

#pragma mark -
#pragma mark setRefresh上拉加载 下拉刷新 Cream
- (void)setupRefresh_cream
{
    [_tableViewCream addHeaderWithTarget:self action:@selector(headerRereshing_cream)];
    
    [_tableViewCream headerBeginRefreshing]; //程序开始即刷新
    
    [_tableViewCream addFooterWithTarget:self action:@selector(footerRereshing_cream)];
    
    _tableViewCream.headerPullToRefreshText = @"下拉刷新啦" ;
    _tableViewCream.headerReleaseToRefreshText = @"松开吧";
    _tableViewCream.headerRefreshingText = @"正在刷新中···";
    
    _tableViewCream.footerPullToRefreshText = @"上拉加载";
    _tableViewCream.footerReleaseToRefreshText = @"松开吧";
    _tableViewCream.footerRefreshingText = @"正在加载中...";
}
// *******************************************

- (void)headerRereshing_cream
{
     [self connect_cream];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_tableViewCream reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tableViewCream headerEndRefreshing];
        NSLog(@"结束刷新");
    });
}
static int cream = 1;
- (void)footerRereshing_cream
{
    NSMutableString * urlstr = [NSMutableString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p%d-s20.html",_bbsid,_r,_o ,cream];
    
    NSRange subStr = [urlstr rangeOfString:[NSString stringWithFormat:@"p%d", cream]];
    cream++;
    if (subStr.location != NSNotFound) {
        [urlstr replaceCharactersInRange:subStr withString:[NSString stringWithFormat:@"p%d", cream]];
    }
    [self connectFooter_new:urlstr];
    
    NSLog(@"%@",urlstr);
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_tableViewCream reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tableViewCream footerEndRefreshing];
        NSLog(@"结束加载");
    });
}
- (void)connectFooter_cream:(NSString *)urlstr
{
    
    
    [NetUrl connect:urlstr parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
        }
        if (![_arrayProvince count]) {
            [self button];
        }
        [_tableViewLast reloadData];
        [_tableViewCream reloadData];
        [_tableViewNew reloadData];
    }];
    
    
}

- (void)connect_last
{


    
    NSString * url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p1-s20.html",_bbsid,_r,_o];
    NSLog(@"%@", url);
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        [_arrayProvince removeAllObjects];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
            [_tableViewLast reloadData];
        }
        
    }];
     

    
}
- (void)connect_new
{
    
    
    NSString * url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p1-s20.html",(long)_bbsid,(long)_r,(long)_o];
    NSLog(@"%@", url);
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        [_arrayProvince removeAllObjects];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
            [_tableViewLast reloadData];
        }
        [_tableViewNew headerEndRefreshing];
        [_tableViewNew footerEndRefreshing];
    }];
}
- (void)connect_cream
{
    
    
    NSString * url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/club/topics-a2-pm1-v4.2.0-b%ld-bto-r%ld-ss0-o%ld-p1-s20.html",_bbsid,_r,_o];
    NSLog(@"%@", url);
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary * dic = (NSMutableDictionary *)data;
        NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
        NSMutableArray * array5 = [dic2 objectForKey:@"list"];
        [_arrayProvince removeAllObjects];
        for (NSMutableDictionary * dic5 in array5) {
            ProvinceModel_province * model = [[ProvinceModel_province alloc]init];
            model.bbsname = [dic5 objectForKey:@"bbsname"];
            model.bbstype = [dic5 objectForKey:@"bbstype"];
            model.bbsid = [dic5 objectForKey:@"bbsid"];
            model.tittle =[dic5 objectForKey:@"title"];
            model.replycount = [dic5 objectForKey:@"replycounts"];
            model.lastreplydate = [dic5 objectForKey:@"lastreplydate"];
            model.topicid = [dic5 objectForKey:@"topicid"];
            model.postusername = [dic5 objectForKey:@"postusername"];
            [_arrayProvince addObject:model];
            [_tableViewLast reloadData];
        }
        [_tableViewCream headerEndRefreshing];
        [_tableViewCream footerEndRefreshing];
    }];
    
}

#pragma mark -
#pragma mark leftBackMethod
- (void)leftBackMethod:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
